import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author : mafan
 * @date : 2020-12-27 18:15
 **/

//给定两个字符串s和，判断它们是否是同构的。
//如果s中的字符可以被替换得到t，那么这两个字符串是同构的。
//所有出现的字符都必须用另一个字符替换，同时保留字符的顺序。两个字符不能映射到同一个字符上，但字符可以映射自己本身。
//输入: s = "egg", t = "add"
//输出: true
public class L1 {
    public  static boolean isIsomorphic(String s, String t) {

        Map<Character, Character> s2t = new HashMap<>();
        Map<Character, Character> t2s = new HashMap<>();
        int n = s.length();
        for (int i = 0; i < n; i++) {
            char key = s.charAt(i), val = t.charAt(i);
            if (!s2t.containsKey(key)) {
                s2t.put(key, val);
            }
            if (!t2s.containsKey(val)) {
                t2s.put(val, key);
            }
            if (s2t.get(key) != val || t2s.get(val) != key) {
                for (Character mp:s2t.keySet()) {
                    System.out.println("s2t:key="+mp+",value="+s2t.keySet());
                }
                for (Character m:t2s.keySet()) {
                    System.out.println("t2s;key="+m+",value="+t2s.keySet());
                }
                return false;
            }
        }
        for (Character mp:s2t.keySet()) {
            System.out.println("s2t:key="+mp+",value="+s2t.keySet());
        }
        for (Character m:t2s.keySet()) {
            System.out.println("t2s;key="+m+",value="+t2s.keySet());
        }
        return true;
    }

    public static boolean is(String s, String t) {
        //判断两个字符串是否长度相等
        if(s.length() != t.length()){
            return false;
        }
        HashMap<Character, Character> map = new HashMap<>();
        for(int i=0; i<s.length(); i++){
            System.out.println("1"+s.charAt(i));
            if(!map.containsKey(s.charAt(i))){
                if(map.containsValue(t.charAt(i))){
                    System.out.println("2"+map);
                    return false;
                }
                map.put(s.charAt(i), t.charAt(i));
                System.out.println("3"+map);
            }else{
                if(map.get(s.charAt(i))!=t.charAt(i)){
                    System.out.println("4"+map);
                    return false;
                }
            }
        }
        return true;
    }
    //我的解答
    public  static boolean Isomorphic(String s, String t) {
        Map<Character, Integer> smap = new LinkedHashMap<>();
        Map<Character, Integer> tmap = new LinkedHashMap<>();
        int[] slist = {};
        for (int i = 0; i < s.length(); i++) {
            if (smap.get(s.charAt(i)) == null) {
                smap.put(s.charAt(i), 0);
            }
            smap.put(s.charAt(i), smap.get(s.charAt(i)) + 1);

            if (tmap.get(t.charAt(i)) == null) {
                tmap.put(t.charAt(i), 0);
            }
            tmap.put(t.charAt(i), tmap.get(t.charAt(i)) + 1);
        }
        for (int i = 0; i < s.length(); i++) {
            if (!smap.get(s.charAt(i)).equals(tmap.get(t.charAt(i)))){
                return false;
            }
        }
        System.out.println(smap);
        System.out.println(tmap);
        return true;
    }

    public  static boolean my(String s, String t) {
        Map<Character,Character> map =new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            map.put(s.charAt(i), t.charAt(i));
            System.out.println(map);
        }
        for (int i = 0; i < s.length(); i++) {
            if (map.get(s.charAt(i))!=t.charAt(i)) {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
//        boolean b = Isomorphic("abba", "abab");
//        boolean b = isIsomorphic("abba", "cdcd");
        boolean b = my("ab", "aa");
        System.out.println(b);
    }
}
